package com.lyr;

import com.lyr.dao.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author liyiruo
 * @Description
 * @Date 2024/8/15 下午3:34
 */
@SpringBootApplication
public class DerbyDBApp {

    private final JdbcTemplate jdbcTemplate;
    public DerbyDBApp(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public static void main(String[] args) {
        SpringApplication.run(DerbyDBApp.class, args);
    }

    @Bean
    public CommandLineRunner demo(UserRepository userRepository) {
        return (args) -> {
            // 创建一个表（如果没有的话）
           // jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY, name VARCHAR(255))");
            userRepository.createTableIfNotExists();
            // 插入一些数据
            jdbcTemplate.update("INSERT INTO users (id, name) VALUES (2, 'John Doe')");
            // 查询用户总数
            int userCount = userRepository.countUsers();
            System.out.println("Total users: " + userCount);
        };
    }
}
